package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Client;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.mapper.ClientMapper;
import cn.wolfcode.crm.query.ClientObjectQuery;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.service.IClientService;
import cn.wolfcode.crm.util.CRMConstant;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class ClientServiceImpl implements IClientService {

    @Autowired
    private ClientMapper clientMapper;

    @Override
    public void save(Client client) {
        Employee currentUser = (Employee) SecurityUtils.getSubject().getPrincipal();
        System.out.println(currentUser);
        client.setInputUser(currentUser);
        client.setCustomerManager(currentUser);
        client.setState(CRMConstant.Client.STATE_NORMAL);
        client.setStateChangeTime(new Date());
        clientMapper.insert(client);
    }

    @Override
    public void delete(Long id) {
        clientMapper.deleteByPrimaryKey(id);

    }

    @Override
    public void update(Client client) {
        clientMapper.updateByPrimaryKey(client);
    }

    @Override
    public Client get(Long id) {
        return clientMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Client> getAll() {
        return clientMapper.selectAll();
    }

    @Override
    public PageResult<Client> query(ClientObjectQuery qo) {
        int totalCount = clientMapper.queryForCount(qo);
        if (totalCount == 0) {
            return new PageResult<Client>(totalCount, Collections.<Client>emptyList());
        }
        return new PageResult<Client>(totalCount, clientMapper.queryForList(qo));
    }

    @Override
    public void changeClientState(Client client) {
        client.setStateChangeTime(new Date());
        clientMapper.changeClientState(client);
    }

    public List<Client> queryForClientWhichCompensate(String keyword) {
        List<Client> clients = clientMapper.queryForClientWhichCompensate(keyword);
        return clients;
    }


    /*查询客户总数*/
    public int selectClientAccount() {
        return clientMapper.selectClientAccount();
    }

    /*查询资源池客户总数*/
    public int selectDClientAccount() {
        return clientMapper.selectDClientAccount();
    }


}
